package com.kele.test.rabbitmq.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@RabbitListener(queues = {"A1"},concurrency = "2")
public class MQService {


    @RabbitHandler
    public void testRevMsg(Map map, Message message, Channel channel
                           ) throws IOException {

        System.out.println(map);
        System.out.println("执行完业务逻辑");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(deliveryTag);

        channel.basicAck(deliveryTag,false);//ACK
    }

    @RabbitHandler
    public void testRevMsg(String body, Message message, Channel channel) throws IOException {

        System.out.println("接收到订单消息" + body
                + "----Thread:"+Thread.currentThread().getId());

        try {
            System.out.println();
            Thread.sleep(10);//模拟业务执行时间，比如数据库操作

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(deliveryTag);
        System.out.println("执行完业务逻辑");
        channel.basicAck(deliveryTag,false);//ACK

    }
}
